//
//  DetailViewController.m
//  liwushuo
//
//  Created by qf1 on 16/4/15.
//  Copyright (c) 2016年 ZJH. All rights reserved.
//

#import "DetailViewController.h"

@interface DetailViewController ()<WKNavigationDelegate,WKUIDelegate>

@end

@implementation DetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText = @"正在加载";
    hud.detailsLabelText = @"请稍后";
    [hud show:YES];
    [self.navigationController.navigationBar setBarTintColor:[UIColor orangeColor]];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = YES;
    self.navigationController.navigationBarHidden = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    self.title = @"攻略详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self getData];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)getData{
    NetWorkManager * manager = [[NetWorkManager alloc]init];
    __weak DetailViewController * weakself = self;
    [manager getDetailDataWithUrl:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/posts/%@",_Id] andBlock:^(id respond) {
        _item = respond;
        WKWebViewConfiguration * configuration = [[WKWebViewConfiguration alloc] init];
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, theScreenWidth, theScreenHeight - 64) configuration:configuration];
        _webView.navigationDelegate = weakself;
        _webView.UIDelegate = weakself;
        _webView.alpha = 0;
        NSMutableString * string = [[NSMutableString alloc] initWithString:_item.content_html];
        while (1) {
            NSRange range = [string rangeOfString:@"查看详情"];
            if (range.location == NSNotFound) {
                break;
            }else
                [string deleteCharactersInRange:range];
        }
        
//        [_webView loadHTMLString:_item.content_html baseURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/posts/391"]];
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_item.content_url]]];
        [self.view addSubview:_webView];
        

    }];
}


- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    _webView.alpha = 1;
    for (int i = 0; i < 50; i ++) {
        [webView evaluateJavaScript:@" document.getElementsByClassName('item-info-link')[0].remove()" completionHandler:nil];
    }
    
    for (int i = 0; i < 50; i ++) {
        [webView evaluateJavaScript:@" document.getElementsByClassName('right-bar')[0].remove()" completionHandler:nil];
    }
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}

- (void)viewWillAppear:(BOOL)animated{
    [self.tabBarController.tabBar setHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
